# -*- encoding=utf8 -*-
__author__ = "Bryan.Chen"

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.std import StdPoco
poco = StdPoco()

using("../FlowTest.air")
import FlowTest

class SubGame_EnterGame(FlowTest.BaseTestCase):

    def __init__(self):
        print("%s __init__" % (__file__))
    
    def runTest(self, args):
        
        gameName = "红包赛"
        isH5 = False
        
        if "gameName" in args:
            gameName = args["gameName"]

        if "isH5" in args:
            isH5 = args["isH5"]
        
        try:
            poco("HjLobbyScene").wait_for_appearance(120)
        except:
            stop_app("com.highlight.mjpe")
        
        try:
            poco("PageView_GameList").wait_for_appearance()
        except: 
            return True
        
        allPageItem = poco("PageView_GameList").children()
        
        def checkGameItemExist(allGameItem):
            isExist = False
            for gameItem in allGameItem:
                txtGameName = gameItem.offspring("game_name").get_text()
                if txtGameName == gameName:
                    self.clickButton(gameItem)
                    isExist = True
                    break
                
            return isExist

        isOpenGame = False
        for pageItem in allPageItem:
            allGameItem = pageItem.children()
            isOpenGame = checkGameItemExist(allGameItem)
            if not isOpenGame:
                poco("PageView_GameList").swipe([-0.5, 0.0])
                sleep(0.5)
            else:
                break
        
        assert_equal(isOpenGame, True, "打开%s" % gameName)

        return True
        
        
_instance = SubGame_EnterGame()
runTest = _instance.runTest

